package com.xxxx.server.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
//import com.xxxx.server.config.CustomAuthorityDeserializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 
 * </p>
 *
 * @author LT
 * @since 2022-02-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("admin")
@ApiModel(value="Admin对象", description="")
public class Admin implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "用户id")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "用户密码")
    private String password;

    @ApiModelProperty(value = "1 男 0女")
    private String sex;

    @ApiModelProperty(value = "用户姓名")
    private String name;

    @ApiModelProperty(value = "电话")
    private String phone;

    @ApiModelProperty(value = "是否启用")
    @Getter(AccessLevel.NONE)//不会生成get方法
    private Boolean enabled;
    @ApiModelProperty(value = "是否启用")
    @TableField(exist = false)//告诉mybatis plus 数据库中没有这个自定义的字段
    private Integer intEnabled;

    @ApiModelProperty(value = "权限")
    @TableField(exist = false)
    private List<Authority> authorityList;

    @Override

    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = authorityList.stream().map(authority -> new SimpleGrantedAuthority(
                        authority.getPermissionName()))
                .collect(Collectors.toList());

        return authorities;
    }

    //账户是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //帐户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //凭据是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //@Data 中有enabled 的get set方法， 在这里又重写一个方法，导致映射时不知道映射哪一个方法
    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
